﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OracleClient;

namespace KPD_Admin
{
    public partial class PerfomanceForm : Form
    {
        DataSet ds = new DataSet();
        string id;
        private OracleCommand cmd;
        private OracleDataAdapter da;
        private OracleCommandBuilder cb;
        string con_string = "Data Source=virtual/orcl;Persist Security Info=True;User ID=KPD;Password=KPD;Unicode=True";

        public PerfomanceForm(string id_val)
        {
            InitializeComponent();
            id = id_val;
        }

        private void PerfomanceForm_Load(object sender, EventArgs e)
        {
            

        }

        private void CalcButton_Click(object sender, EventArgs e)
        {
            DateTime startdate = StartPicker.Value.Date; // Конвертировать тип даты не в запросе, а в коде!!!!!!!
            DateTime enddate = EndPicker.Value.Date;
            DateTime lenght = new DateTime();

            float count = count_days(startdate, enddate);
            float work_rate = get_work_rate(id);
            OracleConnection mycon = new OracleConnection(con_string);
            mycon.Open();
            string sql = "select * from WD_HISTORY WHERE (EMP_ID=" + id + ") AND  (WDH_BEGIN_TIME >= TO_DATE('" + startdate.Date.ToString() + "','DD.MM.YYYY HH24:MI:SS')AND WDH_BEGIN_TIME <= TO_DATE('" + enddate.ToString() + "','DD.MM.YYYY HH24:MI:SS'))";
            
            cmd = new OracleCommand(sql, mycon);
            cmd.CommandType = CommandType.Text;
            da = new OracleDataAdapter(cmd);
            cb = new OracleCommandBuilder(da);
            ds = new DataSet();
            da.Fill(ds);
            dataGridView1.DataSource = ds.Tables[0];
            for (int i = 0; i < dataGridView1.RowCount - 1; i++)
            {
             lenght=lenght+(Convert.ToDateTime(dataGridView1[3,i].Value.ToString())-Convert.ToDateTime(dataGridView1[2,i].Value.ToString()));
            }
            float test = get_hours(lenght);
            float KPD = (get_hours(lenght)/(work_rate * count))*100;


            lenLabel.Text = "КПД составило" + KPD.ToString() + " %";
        }

        private int count_days(DateTime start, DateTime end)
        {
            //DateTime count = end - start;
            //DateTime count = end. - start;
            //int final = Convert.ToInt32(count);
            //return count;
            int count=0;
            while (start!=end)
            {
                start = start.AddDays(1);
                count++;
            }
            return count;
        }
        private float get_work_rate(string id)
        {
            OracleConnection myConnection = new OracleConnection(con_string);
            myConnection.Open();
            cmd = new OracleCommand("select EMP_WORK_RATE from employees where emp_id='"+id+"'", myConnection);
            OracleDataReader myreader = cmd.ExecuteReader();
            myreader.Read();
            float work_rate = myreader.GetInt32(0);
            myConnection.Close();
            return (work_rate/7);
        }
        private float get_hours(DateTime time)
        { 
            float hour = time.Day*24 + time.Hour;
            return hour;
        }
    }
}
